﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommerceOne.Domains.Entities;
using CommerceOne.Domains.Repositories;

namespace CommerceOne.Domains.SqlRepositories
{
    public class SqlProductRepository : IProductRepository
    {
        private readonly EfDbContext _sqlContext = new EfDbContext();

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public IQueryable<Product> GetAll()
        {
            return this._sqlContext.Products;
        }

        /// <summary>
        /// Gets the total item.
        /// </summary>
        /// <returns></returns>
        public int GetTotalItem()
        {
            return this._sqlContext.Products.Count();
        }

        /// <summary>
        /// Gets the total by brand.
        /// </summary>
        /// <param name="brandId">The brand id.</param>
        /// <returns></returns>
        public int GetTotalByBrand(int brandId)
        {
            return this._sqlContext.Products.Where(p => p.BrandId == brandId).Count();
        }

        /// <summary>
        /// Gets the total by department.
        /// </summary>
        /// <param name="departmentId">The department id.</param>
        /// <returns></returns>
        public int GetTotalByDepartment(int departmentId)
        {
            return this._sqlContext.Products.Where(p => p.CategoryId == departmentId).Count();
        }

        /// <summary>
        /// Gets the product.
        /// </summary>
        /// <param name="productId">The product id.</param>
        /// <returns></returns>
        public Product GetProduct(int productId)
        {
            return this._sqlContext.Products.Where(p => p.Id == productId).FirstOrDefault();
        }

        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public List<Product> GetItem(int page, int pageSize)
        {
            int skip = (page - 1)*pageSize;
            return this._sqlContext.Products.OrderByDescending(p => p.AvailableDay).Skip(skip).Take(pageSize).ToList();
        }

        /// <summary>
        /// Gets the total brand.
        /// </summary>
        /// <param name="brandId">The brand id.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public List<Product> GetTotalBrand(int brandId, int page, int pageSize)
        {
            int skip = (page - 1) * pageSize;
            return this._sqlContext.Products.Where(p => p.BrandId == brandId).OrderByDescending(p => p.AvailableDay).Skip(skip).Take(pageSize).ToList();
        }

        /// <summary>
        /// Gets the total department.
        /// </summary>
        /// <param name="departmentId">The department id.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public List<Product> GetTotalDepartment(int departmentId, int page, int pageSize)
        {
            int skip = (page - 1) * pageSize;
            if (skip < 0) skip = 0;
            return this._sqlContext.Products.Where(p => p.CategoryId == departmentId).OrderByDescending(p => p.AvailableDay).Skip(skip).Take(pageSize).ToList();
        }

    }
}
